package com.efast.cafe.portal.service.statemach.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.statemach.PortalStateMachHandle;
import com.efast.cafe.portal.bean.statemach.PortalStateMachHandleQueryBean;
import com.efast.cafe.portal.bean.statemach.PortalStateMachRelBean;
import com.efast.cafe.portal.dao.statemach.PortalStateMachHandleDao;
import com.efast.cafe.portal.service.statemach.IPortalStateMachHandleService;
import com.efast.cafe.portal.service.statemach.IPortalStateMachRelService;

@Service
@Transactional
public class PortalStateMachHandleServiceImpl implements IPortalStateMachHandleService {

	@Autowired
	private PortalStateMachHandleDao portalStateMachHandleDao;

	@Autowired
	private ObjectRedisTemplate objectRedisTemplate;

	@Autowired
	private IPortalStateMachRelService portalStateMachRelService;

	@Override
	public void insertHandle(PortalStateMachHandle handle) {
		portalStateMachHandleDao.insertHandle(handle);
	}

	@Override
	public void updateHandle(PortalStateMachHandle handle) {
		portalStateMachHandleDao.updateHandle(handle);
		// 查找受影响的confId清除redis缓存
		List<PortalStateMachRelBean> relList = portalStateMachRelService.queryRelListByPara(handle.getid(), null);
		for (PortalStateMachRelBean rel : relList) {
			objectRedisTemplate.delete(CommonConstant.REDIS_STATEMACH_CONFID_ + rel.getConfId());
		}
	}

	@Override
	public void deleteHandle(PortalStateMachHandle handle) {
		portalStateMachHandleDao.deleteHandle(handle);
		// 查找受影响的confId清除redis缓存
		List<PortalStateMachRelBean> relList = portalStateMachRelService.queryRelListByPara(handle.getid(), null);
		for (PortalStateMachRelBean rel : relList) {
			objectRedisTemplate.delete(CommonConstant.REDIS_STATEMACH_CONFID_ + rel.getConfId());
		}
	}

	@Override
	public PortalStateMachHandleQueryBean queryPageStateMachHandle(PortalStateMachHandleQueryBean queryBean) {
		queryBean.setCompanyCode(SpringWebUtils.getRequestCompany());
		queryBean.setResultList(portalStateMachHandleDao.queryHandlePageList(queryBean));
		queryBean.setTotalRows(portalStateMachHandleDao.queryHandlePageCount(queryBean));
		return queryBean;
	}

	@Override
	public void deleteHandleByConfId(Long confId) {
		portalStateMachHandleDao.deleteHandleByConfId(confId);
	}

}
